package com.dk.controller;

import com.dk.pojo.TbAreas;
import com.dk.pojo.TbCities;
import com.dk.pojo.TbOnsiteAssessment;
import com.dk.pojo.TbProvinces;
import com.dk.result.Result;
import com.dk.service.AssessmentService;
import com.dk.service.TbAreasService;
import com.dk.service.TbCitiesService;
import com.dk.service.TbProvincesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RefreshScope//自动刷新
@RequestMapping("assessment")
@Api
@ApiOperation("上门评估接口")
public class AssessmentController {


    @Autowired
    private AssessmentService assessmentService;

    @Autowired
    private TbProvincesService provincesService;

    @Autowired
    private TbCitiesService citiesService;

    @Autowired
    private TbAreasService areasService;


    @GetMapping("getAssessmentList")
    @ApiOperation(value = "查询全部")
    public Result getAssessmentList(){
        try {
            List<TbOnsiteAssessment> assessmentList = assessmentService.getAssessmentList();
            return Result.OK().setData(assessmentList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }

    }


    @GetMapping("/getAssessment/{id}")
    @ApiOperation(value = "根据id查询")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, paramType = "path")
    public Result getAssessment(@PathVariable Long id){
        try {
            TbOnsiteAssessment assessment = assessmentService.selectById(id);
            return Result.OK().setData(assessment);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }


    @PostMapping("addAssessment")
    @ApiOperation(value = "增加")
    @ApiImplicitParam(name = "assessment", value = "上门评估实体", required = true, paramType = "body")
    public Result addAssessment(@RequestBody TbOnsiteAssessment assessment){
        try {
            assessmentService.insertAssessment(assessment);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }

    }


    @PutMapping("updateStatusById")
    @ApiOperation(value = "修改状态为已上门 1")
    @ApiImplicitParam(name = "assessment", value = "上门评估实体", required = true, paramType = "body")
    public Result updateStatusById(@RequestBody TbOnsiteAssessment assessment){
        try {
            assessmentService.updateStatusById(assessment);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }


    @PutMapping("updateAssessment")
    @ApiOperation(value = "修改")
    @ApiImplicitParam(name = "assessment", value = "上门评估实体", required = true, paramType = "body")
    public Result updateAssessment(@RequestBody TbOnsiteAssessment assessment){
        try {
            assessmentService.update(assessment);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }





    @DeleteMapping("deleteById/{id}")
    @ApiOperation(value = "根据id删除")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, paramType = "path")
    public Result deleteById(@PathVariable Long id){
        try {
            assessmentService.deleteById(id);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }


    @GetMapping("getProvinceList")
    @ApiOperation(value = "查询全部省份")
    public Result getProvinceList(){
        try {
            List<TbProvinces> provinces = provincesService.getProvinceList();
            return Result.OK().setData(provinces);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }


    @GetMapping("selectCityByPid/{provinceId}")
    @ApiOperation(value = "根据省份id查询该省份的市")
    @ApiImplicitParam(name = "provinceId", value = "省份id", required = true, paramType = "path")
    public Result selectCityByPid(@PathVariable Long provinceId){
        try {
            List<TbCities> cities = citiesService.selectCityByPid(provinceId);
            return Result.OK().setData(cities);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }


    @GetMapping("selectAreaByCid/{cityId}")
    @ApiOperation(value = "根据省市id查询该市的区域/县")
    @ApiImplicitParam(name = "cityId", value = "省市id", required = true, paramType = "path")
    public Result selectAreaByCid(@PathVariable Long cityId){
        try {
            List<TbAreas> areas = areasService.selectAreaByCid(cityId);
            return Result.OK().setData(areas);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }

}
